Ext.define('cloud.controller.VideosController', {
    extend: 'Ext.app.Controller',
    requires: ['Ext.picker.Picker'],
    proID:'',
    config: {
        file: '',
        urldata: [],
        filedata: [],
        refs: {
            videos: 'videos',
            videosAdd: 'videosAdd',
            videosEdit:'videosEdit',
            videos_edit:'videosEdit #videos_edit',
            videos_del:'videosEdit #videos_del',
            videos_add: 'videos #videos_add',
            videos_submit: 'videosAdd #videos_submit',
//
            videos_upload: 'videosAdd #videos_upload',
            videos_upload_two: 'videosAdd #videos_upload_two',

            videos_edit_upload: 'videosEdit #videos_edit_upload',
            videos_edit_upload_two: 'videosEdit #videos_edit_upload_two'

        },
        control: {
            videos_add: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var videos = this.getVideos();
                    var videosAdd = this.getVideosAdd()||Ext.create('cloud.view.videos.VideosAdd');
                    Ext.Viewport.unmask();
                    videos.push(videosAdd);
                }
            },
            /**
             * upload
             */
            videos_upload: {
                loadsuccess: function (dataurl, m, e, file) {
                    var img_name = 'videos_loadedImage';
                    file['display'] = 1;
                    this.upload(dataurl, m, e, file, img_name);
                }
            },
            videos_upload_two: {
                loadsuccess: function (dataurl, m, e, file) {
                    var img_name = 'videos_loadedImage_two';
                    file['display'] = 2;
                    this.uploadVideo(dataurl, m, e, file, img_name);
                }
            },

            videos_edit_upload: {
                loadsuccess: function (dataurl, m, e, file) {
                    var img_name = 'videos_edit_loadedImage';
                    file['display'] = 1;
                    this.upload(dataurl, m, e, file, img_name);
                }
            },
            videos_edit_upload_two: {
                loadsuccess: function (dataurl, m, e, file) {
                    var img_name = 'videos_edit_loadedImage_two';
                    file['display'] = 2;
                    this.uploadVideo(dataurl, m, e, file, img_name);
                }
            },

            'videos list': {
                itemtap: function (list, item, index, record) {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var videos = this.getVideos();
                    var videosEdit = this.getVideosEdit()||Ext.create('cloud.view.videos.VideosEdit');
                    Ext.Ajax.request({
                        url: GLOBAL.api_url + 'videos_info_get',
                        params: {id: record.get('id')},
                        scope: this,
                        success: function (response) {
                            var result = Ext.decode(response.responseText);
                            if (result.Variables.Result.code == 0) {
                                var data = result.Variables.data;
                                var model=Ext.define('Videos', {
                                    extend: 'Ext.data.Model',
                                    config: {
                                        fields:["id","title","centext","imageurl","videourl","display",'lasttime']
                                    }
                                });
                                var recorddata=Ext.create('Videos',data);
                                var videos = data.videourl;
                                Ext.getCmp('video_id').setValue(data.id);
                                videosEdit.setRecord(recorddata);
                                Ext.getCmp('videos_edit_loadedImage').setSrc(data.imageurl);
                                if (!Ext.isEmpty(videos)) {
                                   Ext.getCmp('videos_edit_loadedImage_two').setHtml(videos.substr(videos.lastIndexOf("/")+1)) ;

                                }
                                Ext.Viewport.unmask();
                            }
                        }
                    });
                    videos.push(videosEdit);
                }
            },
            videos_submit: {
                tap: function () {
                    if (Ext.isEmpty(Ext.getCmp('videos_title').getValue()))
                        return Ext.Msg.alert('请输入标题');
                    if (Ext.isEmpty(Ext.getCmp('videos_content').getValue()))
                        return Ext.Msg.alert('请输入内容');
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var filedata = this.getFiledata();
                    var filelen = filedata.length;
                    if (filelen > 0) {
                        var looplen = 1;
                        var subthis = this;
                        console.log(this);
                        Ext.each(filedata, function (file) {
                            var http = new XMLHttpRequest();
                            var form = new FormData();
                            form.append('Filedata', file);
                              var url = GLOBAL.api_url + 'attachment_upload&updatatype=article&display=' + file['display'];
                            if (http.upload && http.upload.addEventListener) {
                                // Response handler
                                http.onreadystatechange = function (e) {
                                    if (this.readyState == 4) {
                                        if (this.status == 200) {
                                            var returnText = Ext.decode(this.responseText);
                                            if (returnText.Variables.Result.code == 0) {
                                                     if(file['display']==1){
                                                         Ext.getCmp('videos_imageurl').setValue(returnText.Variables.data.attachdir);
                                                    }else{
                                                         Ext.getCmp('videos_videourl').setValue(returnText.Variables.data.attachdir);
                                                    }
                                                if(looplen >= filelen){
                                                     subthis.getVideosAdd().submit({
                                                        url: GLOBAL.api_url + 'videos_info_add',
                                                        scope: this,
                                                        method: 'POST',
                                                        success: function(from, result){
                                                            if (result.Variables.Result.code == 0) {
                                                                Ext.Viewport.unmask();
                                                                Ext.getStore('VideosStore').load();
                                                                subthis.getVideos().pop();
                                                            } else {
                                                                Ext.Viewport.unmask();
                                                                Ext.Msg.alert(result.Variables.Message.messagestr);
                                                            }
                                                        },
                                                        failure:function(){
                                                            Ext.Viewport.unmask();
                                                        }
                                                    });
                                                }
                                                    looplen = looplen + 1;

                                            }else{
                                                Ext.Viewport.unmask();
                                                Ext.Msg.alert(result.Variables.Message.messagestr);
                                            }
                                        }
                                    }
                                };
                                // Error handler
                                http.upload.onerror = this.actionfailure;
                            }
                            http.open('POST', url);
                            http.send(form);

                        });
                        this.setFiledata([]);
                    }else{
                        Ext.Viewport.unmask();
                        return Ext.Msg.alert('请上传文件');
                    }




                }
            },

            videos_edit: {
                tap: function () {
                    if (Ext.isEmpty(Ext.getCmp('videos_title_edit').getValue()))
                        return Ext.Msg.alert('请输入标题');
                    if (Ext.isEmpty(Ext.getCmp('videos_content_edit').getValue()))
                        return Ext.Msg.alert('请输入内容');
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var filedata = this.getFiledata();
                    var filelen = filedata.length;
                    if (filelen > 0) {
                        var looplen = 1;
                        var subthis = this;
                        console.log(this);
                        Ext.each(filedata, function (file) {
                            var http = new XMLHttpRequest();
                            var form = new FormData();
                            form.append('Filedata', file);
                            var url = GLOBAL.api_url + 'attachment_upload&updatatype=article&display=' + file['display'];
                            if (http.upload && http.upload.addEventListener) {
                                // Response handler
                                http.onreadystatechange = function (e) {
                                    if (this.readyState == 4) {
                                        if (this.status == 200) {
                                            var returnText = Ext.decode(this.responseText);
                                            if (returnText.Variables.Result.code == 0) {
                                                if(file['display']==1){
                                                    Ext.getCmp('videos_imageurl').setValue(returnText.Variables.data.attachdir);
                                                }else{
                                                    Ext.getCmp('videos_videourl').setValue(returnText.Variables.data.attachdir);
                                                }
                                                if(looplen >= filelen){
                                                    console.log(subthis);
                                                    subthis.getVideosEdit().submit({
                                                        url: GLOBAL.api_url + 'videos_info_update',
                                                        scope: this,
                                                        method: 'POST',
                                                        params:{id:Ext.getCmp('video_id').getValue()},
                                                        success: function(from, result){
                                                            if (result.Variables.Result.code == 0) {
                                                                Ext.Viewport.unmask();
                                                                Ext.getStore('VideosStore').load();
                                                                subthis.getVideos().pop();
                                                            } else {
                                                                Ext.Viewport.unmask();
                                                                Ext.Msg.alert(result.Variables.Message.messagestr);
                                                            }
                                                        },
                                                        failure:function(){
                                                            Ext.Viewport.unmask();
                                                        }
                                                    });
                                                }
                                                looplen = looplen + 1;

                                            }else{
                                                Ext.Viewport.unmask();
                                                Ext.Msg.alert(result.Variables.Message.messagestr);
                                            }
                                        }
                                    }
                                };
                                // Error handler
                                http.upload.onerror = this.actionfailure;
                            }
                            http.open('POST', url);
                            http.send(form);

                        });
                        this.setFiledata([]);
                    }else{
                        this.getVideosEdit().submit({
                            url: GLOBAL.api_url + 'videos_info_update',
                            scope: this,
                            method: 'POST',
                            params:{id:Ext.getCmp('video_id').getValue()},
                            success: function(from, result){
                                if (result.Variables.Result.code == 0) {
                                    Ext.Viewport.unmask();
                                    Ext.getStore('VideosStore').load();
                                    this.getVideos().pop();
                                } else {
                                    Ext.Viewport.unmask();
                                    Ext.Msg.alert(result.Variables.Message.messagestr);
                                }
                            },
                            failure:function(){
                                Ext.Viewport.unmask();
                            }
                        });
                    }


                }
            },
            videos_del: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var subthis=this;
                    this.getVideosEdit().submit({
                        url: GLOBAL.api_url + 'videos_info_delete',
                        scope: this,
                        method: 'POST',
                        params:{id:Ext.getCmp('video_id').getValue()},
                        success: function (from, result) {
                            Ext.Viewport.unmask();
                            if (result.Variables.Result.code == 0) {
                                Ext.Viewport.unmask();
                                Ext.getStore('VideosStore').load();
                                subthis.getVideos().pop();
                            } else {
                                Ext.Msg.alert(result.Variables.Message.messagestr);
                            }
                        }
                    });
                }
            }
        }
    },

    upload: function (dataurl, m, e, file, img_name, display) {
        var me = this;
        this.setFile(file);
        var display = display;
        image = [];
        // 储存file
        files = this.getFiledata();
        files.push(file);
        this.setFiledata(files);
        var image = Ext.getCmp(img_name);
        image.setSrc(dataurl);
    },


    uploadVideo: function (dataurl, m, e, file, img_name, display) {
        var me = this;
        this.setFile(file);
        var display = display;
        image = [];
        // 储存file
        files = this.getFiledata();
        files.push(file);
        console.log(file)
        this.setFiledata(files);
        var video = Ext.getCmp(img_name);
        video.setHtml(file.name);
    },
    executeSuccess: function (from, result) {
        if (result.Variables.Result.code == 0) {
            Ext.Viewport.unmask();
            Ext.getStore('VideosStore').load();
            this.getVideos().pop();
        } else {
            Ext.Viewport.unmask();
            Ext.Msg.alert(result.Variables.Message.messagestr);
        }
    },
    actionfailure: function () {//请求失败时执行操作
        Ext.Viewport.unmask();
        Ext.Msg.alert('请求失败', '服务器维护中....');
    }


});